<template>
  <div class="order">
    <div class="order_price">实付金额<b>&yen;{{ calculations.price }}</b></div>
    <div class="order_btn" @click="()=>handleSubmitClick(true)">提交订单</div>
  </div>
  <div
    class="mask"
    @click="()=>handleSubmitClick(false)"
    v-show="showConfirm"
  >
    <div class="mask_content" @click.stop>
      <h3 class="mask_content_title">确认要离开收银台？</h3>
      <p class="mask_content_desc">请尽快完成支付，否则将被取消</p>
      <div class="mask_content_btns">
        <div class="mask_content_btn mask_content_btn--first" @click="handleCancelOrder">取消订单</div>
        <div class="mask_content_btn mask_content_btn--last" @click="handleConfirmOrder">确认支付</div>
      </div>
    </div>
  </div>
</template>

<script>
import { useStore } from 'vuex'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useCommonCartEffect } from '@/effect/cartEffects'
import { post } from '@/utils/request'

// 提交订单的计算
const useMakeOrderEffect = (productList, shopName, shopId) => {
  const store = useStore()
  const router = useRouter()
  const handleConfirmOrder = async () => {
    const products = []
    for (const i in productList.value) {
      const product = productList.value[i]
      products.push({
        id: parseInt(product._id, 10),
        num: product.count
      })
    }
    try {
      const result = await post('/api/order', {
        addressId: 1,
        shopName: shopName.value,
        shopId: shopId,
        isCanceled: false,
        products
      })
      if (result?.code === 2000) {
        store.commit('clearCartData', shopId)
        router.push({ name: 'OrderList' })
      } else {
      }
    } catch (e) {
      console.log('付款失败', e)
    }
  }

  return {
    handleConfirmOrder
  }
}
// 蒙层相关的计算
const useShowMaskEffect = () => {
  const showConfirm = ref(false)
  const handleCancelOrder = () => {
    showConfirm.value = false
  }
  const handleSubmitClick = (statue) => {
    showConfirm.value = statue
  }
  return {
    showConfirm,
    handleCancelOrder,
    handleSubmitClick
  }
}

export default {
  name: 'Order',
  setup () {
    const route = useRoute()
    const shopId = parseInt(route.params.id, 10)

    const { calculations, shopName, productList } = useCommonCartEffect(shopId)
    const { handleConfirmOrder } = useMakeOrderEffect(productList, shopName, shopId)
    const { showConfirm, handleCancelOrder, handleSubmitClick } = useShowMaskEffect()
    return {
      calculations,
      showConfirm,
      handleCancelOrder,
      handleConfirmOrder,
      handleSubmitClick
    }
  }
}
</script>
<style lang="scss" scoped>
@import "../../style/viriables";
@import "../../style/mixins";

.order {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  height: .49rem;
  line-height: .49rem;
  background: $bgColor;

  &_price {
    flex: 1;
    text-indent: .24rem;
    font-size: .14rem;
    color: $content-fontcolor;
  }

  &_btn {
    width: .98rem;
    color: $bgColor;
    background: #4FB0F9;
    font-size: 14px;
    text-align: center;
  }
}

.mask {
  z-index: 1;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgb(0, 0, 0, 0.50);

  &_content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 3rem;
    height: 1.56rem;
    background: #FFF;
    text-align: center;
    border-radius: .04rem;

    &_title {
      margin: .24rem 0 0 0;
      line-height: .26rem;
      font-size: .18rem;
      color: #333333;
    }

    &_desc {
      margin: .08rem 0 0 0;
      font-size: .14rem;
      color: #666666;
    }

    &_btns {
      display: flex;
      margin: .24rem .58rem;
    }

    &_btn {
      flex: 1;
      width: .8rem;
      line-height: .32rem;
      border: .01rem solid #4FB0F9;
      border-radius: .16rem;
      font-size: .14rem;

      &--first {
        margin-right: .12rem;
        border: .01rem solid #4FB0F9;
        color: #4FB0F9;
      }

      &--last {
        margin-left: .12rem;
        background: #4FB0F9;
        color: #FFF;
      }
    }
  }
}
</style>
